package com.eastdigit.bpm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.form.FormData;
import org.activiti.engine.form.FormProperty;

import net.sf.json.JSONObject;

import com.eastdigit.bpm.model.FormEntity;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.system.service.SpringServiceFactory;
import com.eastdigit.util.StringUtil;

/**
 * Created by Administrator on 2015/10/27.
 */
public class FormFactory {
	
	
    /**
     * 获取表单参数
     * 
     * @param properties
     * @return
     */
    public static Map<String, String> getProperties(String properties) {
        return getProperties(JSONObject.fromObject(properties));
    }
    
    /**
     * 获取表单参数
     * 
     * @param properties
     * @return
     */
    public static Map<String, String> getProperties(JSONObject properties) {
        Map<String, String> variables = new HashMap<String, String>();
        if (properties != null) {
            for (Object key : properties.keySet()) {
                variables.put((String) key, properties.getString((String) key));
            }
        }
        return variables;
    }

    /**
     * 从输入数据获取业务服务
     * 
     * @param properties
     * @return
     * @throws Exception
     */
	public static IFormService<FormEntity> getFormService(
			Map<String, String> properties) throws Exception {
		return getFormService(getServiceName(properties));
	}
	
	public static IFormService<FormEntity> getFormServiceByKey(String bussinessKey) throws Exception {
		return getFormService(getServiceName(bussinessKey));
	}
	
	@SuppressWarnings("unchecked")
	public static IFormService<FormEntity> getFormService(String serviceName) {
		IFormService<FormEntity> formService = null;
		if(!StringUtil.isEmpty(serviceName)){
			formService = (IFormService<FormEntity>) SpringServiceFactory.getBean(serviceName);
			if (formService == null) {
				throw new BusinessException("没查询到业务服务！");
			}
		}
		return formService;
	}

    /**
     * 获取业务service对象名
     * 
     * @param properties
     * @return
     * @throws Exception
     */
    public static String getServiceName(Map<String, String> properties) {
        String beanName = properties.get("_service");
        if (StringUtil.isEmpty(beanName)) {
            beanName = "dZFormService";
        }
        return beanName;
    }
    
    public static String buildBussinessKey(String serviceName, String businessId){
    	return (StringUtil.isEmpty(serviceName) ? "dZFormService" : serviceName ) + ":" + businessId;
    }
    
    public static String getServiceName(String bussinessKey) {
    	String serviceName = null;
    	if (!StringUtil.isEmpty(bussinessKey)) {
            String[] keys = bussinessKey.split(":");
            if (keys.length == 2) {
            	serviceName = keys[0];
            }
    	}
    	return serviceName;
    }
    
    public static String getBusinessId(String businessKey) {
    	String businessId = businessKey;
    	if (!StringUtil.isEmpty(businessKey)) {
            String[] keys = businessKey.split(":");
            if (keys.length == 2) {
            	businessId = keys[1];
            }
    	}
    	return businessId;
    }
    
    
    /**
     * 获取表单
     * 
     * @param formData
     * @return
     */
    public static JSONObject getForm(FormData formData) {
        JSONObject form = new JSONObject();
        if (formData != null) {
            List<FormProperty> properties = formData.getFormProperties();
            if (properties != null && properties.size() > 0) {
                form.put("properties", properties);
            }
            if (formData.getFormKey() != null) {
                form.put("key", formData.getFormKey());
            }
        }
        return form;
    }

    /**
     * 通过businessKey获取业务表单数据
     * 
     * @param bussinessKey
     * @return
     */
    public static JSONObject getFormData(String businessKey) {
    	JSONObject data = new JSONObject();
    	IFormService<FormEntity> service = getFormService(getServiceName(businessKey));
    	String businessId = getBusinessId(businessKey);
    	if(service != null && !StringUtil.isEmpty(businessId)){
    		data = service.toJSONObject(businessId);
    	}
    	return data;
    }

}
